<template>
	<view class="container">
		<view class="content">
			<u-loading-page :loading="isLoading" loading-text="加载中......" loading-mode="semicircle"></u-loading-page>
			<view class="content-back-groud">
				<view style="width: 100%;height: 70%;display: flex;">
					<image style="width: 80px;height: 60px;margin-top: 10px;"
						src="https://www.taotaohomecraft.press/prod-api/profile/vip.png"></image>
					<image style="width: 130px;height: 110px;margin-left: 120px;margin-top: 15px;"
						src="https://www.taotaohomecraft.press/prod-api/profile/member-icon.png">
					</image>
				</view>
				<text style="font-size:18px;margin-top: 18px;margin-left: 20px;">开通会员立即解锁借阅权益</text>
			</view>
			<view class="content-recharge">
				<view v-for="(item, index) in list" :key="item.id" class="content-recharge-card"
					:class="{ 'active': selectedId === item.id }" @click="handleCardClick(item)">
					<text style="font-size: 22px;margin-bottom: 20px;">{{item.typeName}}</text>
					<u-icon name="rmb" color="#FF0000" size="17" :label="item.denomination" labelSize="25"
						labelColor="#FF0000"></u-icon>
				</view>
			</view>
		</view>
		<view class="footer-button">
			<view style="width: 200px;display: flex; flex-direction: column;">
				<text style="font-size: 22px;;margin-top: 5px;">{{totalAmount}} /元</text>
				<text style="margin-bottom: 10px;color: #FF0000">{{remark}}</text>
			</view>
			<view
				style="width: 120px;margin-left: 15%;display: flex;flex-direction: column;align-items: center; justify-content: center;">
				<u-button :loading="buttonLoading" type="primary" shape="circle" color="#FFA942" text="立即开通"
					@click="handleCheckout"></u-button>
			</view>
		</view>
		<view>
			<u-modal content="是否开通该会员？" :show="payShow" showCancelButton closeOnClickOverlay
				@confirm="confirmCreateOrder" @cancel="cancelPayShow()" @close="cancelPayShow()"></u-modal>
		</view>
	</view>
</template>

<script>
	import {
		createPreOrder
	} from '@/api/order';
	import {
		listInfo
	} from '@/api/member';
	import {
		onPullDownRefresh
	} from '@dcloudio/uni-app';
	export default {
		data() {
			return {
				isLoding: false,
				buttonLoading: false,
				totalAmount: '0.00',
				type: '1',
				remark: '',
				payShow: false,
				selectedId: null,
				list: []
			}
		},
		onload() {
			this.getList()
		},
		onPullDownRefresh() {
			// 模拟数据请求
			setTimeout(() => {
				this.getList()
				uni.stopPullDownRefresh(); // 停止刷新动画（必须调用！）
			}, 1000);
		},
		mounted() {
			this.getList()
		},
		methods: {
			async getList() {
				console.log('走没走啊')
				this.isLoading = true
				const res = await listInfo();
				if (res.code === 200) {
					console.log('resres', res)
					this.list = res.rows
				}
				// setTimeout(() => {
				// 	listInfo().then(res => {
				// 		if (res.code === 200) {
				// 			console.log('resres', res)
				// 			this.list = res.rows
				// 		}
				// 	})
				// 	this.isLoading = false
			},
			handleCardClick(row) {
				console.log('row', row)
				this.selectedId = row.id
				this.remark = row.remark
				this.totalAmount = row.denomination
			},
			handleCheckout() {
				if (this.totalAmount == '0.00') {
					uni.showToast({
						icon: 'none',
						title: '请选择会员卡',
						duration: 1000,
					});
					return false
				}
				this.payShow = true
			},
			cancelPayShow() {
				this.payShow = false
			},
			async confirmCreateOrder() {

				this.payShow = false
				this.buttonLoading = true
				let data = {
					totalPrice: this.totalAmount,
					goodsName: '会员卡购买',
					memberCardId: this.selectedId,
					type: this.type
				}
				const res = await createPreOrder(data);
				if (res.code === 200) {
					console.log("预下单成功", res)
					uni.requestPayment({
						provider: 'wxpay',
						timeStamp: res.data.timeStamp,
						nonceStr: res.data.nonceStr,
						package: res.data.prepayId,
						signType: 'RSA',
						paySign: res.data.paySign,
						success: function(res) {
							console.log(res)
							uni.showToast({
								title: '支付成功',
								duration: 1000
							});
							uni.switchTab({
								url: '/pages/mine/index'
							})
						},
						fail: function(err) {
							console.log(err)
							uni.showToast({
								title: '支付失败',
								icon: 'none',
								duration: 2500
							});
							console.log('fail:' + JSON.stringify(err));
						}
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	.container {
		position: relative;
		height: 100vh;
		width: 100%;
		background-color: #F5F5F5;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.content {
		width: 93%;
		height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.content-back-groud {
		width: 100%;
		margin-top: 5px;
		background-image: url('https://www.taotaohomecraft.press/prod-api/profile/member-backgroud.png');
		height: 200px;
		border-radius: 25px;
		/* 新增以下属性 */
		background-size: cover;
		background-repeat: no-repeat;
		background-position: center;
		display: flex;
		flex-direction: column;
	}

	.content-recharge {
		display: flex;
		flex-wrap: wrap;
		gap: 10px;
		width: 100%;
		margin-top: 10px;
	}

	.content-recharge-card {
		width: 105px;
		height: 125px;
		border-radius: 25px;
		background-color: #FFFFFF;
		flex-shrink: 0;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	/* 选中状态样式 */
	.content-recharge-card.active {
		background-color: #FFA942;
		/* 新的颜色值 */
		box-shadow: 0 0 10px rgba(255, 169, 66, 0.5);
		/* 同步调整阴影色相 */
		color: #FFF;
	}

	.content-pay {
		width: 100%;
		height: 6%;
		margin-top: 5%;
		background-color: #FFFFFF;
		display: flex;
	}

	.footer-button {
		display: flex;
		// z-index: 999;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		background: #fff;
		/* 按钮背景色 */
		padding: 10px;
		box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
	}
</style>